package com.jiajia.study.product.controller;

import com.jiajia.common.utils.PageUtils;
import com.jiajia.common.utils.R;
import com.jiajia.study.product.entity.AttrEntity;
import com.jiajia.study.product.entity.ProductAttrValueEntity;
import com.jiajia.study.product.enums.AttrTypeEnum;
import com.jiajia.study.product.service.AttrService;
import com.jiajia.study.product.service.ProductAttrValueService;
import com.jiajia.study.product.vo.AttrRestVO;
import com.jiajia.study.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author zjj
 * @email zjj@qq.com
 * @date 2025-06-09 15:37:17
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 规格参数列表
     * @param params    分页参数
     * @param catelogId 分类id
     * @return          分页数据
     */
    @GetMapping("/base/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId, AttrTypeEnum.BASE_ATTR.getType());

        return R.ok().put("page", page);
    }

    @GetMapping("base/listforspu/{spuId}")
    public R spuListBySpuId(@PathVariable("spuId")Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.valueBySpuId(spuId);
        return R.ok().put("data",productAttrValueEntityList);
    }

    @RequestMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId")Long spuId,@RequestBody List<ProductAttrValueEntity> productAttrValueEntityList){
        productAttrValueService.updateSpuAttr(spuId,productAttrValueEntityList);
        return R.ok();
    }

    @GetMapping("/sale/list/{catelogId}")
    public R saleAttrList(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,AttrTypeEnum.SALE_ATTR.getType());

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRestVO attr = attrService.getInfoById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
//    @RequestMapping("/save")
//    public R save(@RequestBody AttrEntity attr){
//		attrService.save(attr);
//
//        return R.ok();
//    }
    @RequestMapping("save")
    public R save(@RequestBody AttrVO attrVO){
        attrService.saveAttr(attrVO);
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attr){
		attrService.updateInfoById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
